package com.carl.zlds.service.impl;

import com.carl.zlds.tool.common.PageBO;
import com.carl.zlds.tool.util.AssertUtil;
import com.carl.zlds.tool.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.zlds.dao.GameTagInfoMapper;
import com.carl.zlds.model.GameTag;
import com.carl.zlds.model.GameTagInfo;
import com.carl.zlds.pojo.dto.SaveGameTagInfoDTO;
import com.carl.zlds.pojo.vo.GameTagInfoVO;
import com.carl.zlds.pojo.vo.GameTagVO;
import com.carl.zlds.service.IGameTagInfoService;
import com.carl.zlds.service.IGameTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author carl
 */
@Slf4j
@Service
public class GameTagInfoServiceImpl extends ServiceImpl<GameTagInfoMapper, GameTagInfo> implements IGameTagInfoService {

    private final IGameTagService gameTagService;

    public GameTagInfoServiceImpl(IGameTagService gameTagService) {
        this.gameTagService = gameTagService;
    }

    @Override
    public void saveGameTagInfo(Long id, SaveGameTagInfoDTO dto) {
        GameTagInfo gameTagInfo;
        String name = dto.getName();
        Long gameTagId = dto.getGameTagId();

        GameTag gameTag = gameTagService.getOne(new QueryWrapper<GameTag>().eq("id", gameTagId).eq("weight", 0));
        AssertUtil.isNull("游戏标签分类不存在", gameTag);

        if (id > 0) {
            gameTagInfo = this.baseMapper.selectOne(new QueryWrapper<GameTagInfo>().eq("id", id).eq("weight", 0));
            AssertUtil.isNull("游戏标签不存在", gameTagInfo);
            Wrapper<GameTagInfo> wrapper = new QueryWrapper<GameTagInfo>().eq("name", name)
                    .eq("game_tag_id", gameTagId)
                    .eq("weight", 0)
                    .ne("id", id);
            GameTagInfo gti = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("游戏标签已存在", gti);
        } else {
            Wrapper<GameTagInfo> wrapper = new QueryWrapper<GameTagInfo>().eq("id", id)
                    .eq("name", name)
                    .eq("game_tag_id", gameTagId)
                    .eq("weight", 0);
            GameTagInfo gti = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("游戏标签已存在", gti);
            gameTagInfo = new GameTagInfo();
        }

        gameTagInfo.setName(name);
        gameTagInfo.setGameTagId(gameTagId);
        gameTagInfo.setParentId(dto.getParentId());
        gameTagInfo.setSort(dto.getSort());

        if (id > 0) {
            this.baseMapper.updateById(gameTagInfo);
        } else {
            this.baseMapper.insert(gameTagInfo);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteGameTagInfo(Long id) {
        Wrapper<GameTagInfo> wrapper = new QueryWrapper<GameTagInfo>().eq("id", id).eq("weight", 0);
        GameTagInfo gameTagInfo = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("游戏标签不存在", gameTagInfo);

        gameTagInfo.setWeight(-1);
        this.baseMapper.updateById(gameTagInfo);
        this.baseMapper.deleteByParentId(id);
    }

    @Override
    public List<GameTagInfo> getGameTagInfoList(Long gameTagId, Long parentId) {
        QueryWrapper<GameTagInfo> wrapper = new QueryWrapper<GameTagInfo>()
                .eq("game_tag_id", gameTagId)
                .eq("weight", 0);
        if (parentId > 0) {
            wrapper.in("parent_id", 0, parentId);
        } else if (parentId == 0) {
            wrapper.eq("parent_id", 0);
        }
        wrapper.orderByAsc("sort");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<GameTagInfoVO> getGameTagInfoListByIdList(List<Long> idList) {
        return this.baseMapper.getGameTagInfoListByIdList(idList);
    }
}
